package cn.glutnn.controller;

import cn.glutnn.entity.Result;
import cn.glutnn.utils.AliyunOSS;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @Title: FileUpLoadController
 * @Author QJQ
 * @Package cn.glutnn.controller
 * @Date 2024/11/1 11:01
 * @description: 文件上传
 */
@Tag(name = "头像及文件上传模块")
@RestController
@RequestMapping("/file")
public class FileUpLoadController {

    @Operation(summary = "上传头像")
    @PostMapping("/uploadAvatar")
    public Result<String> uploadAvatar(MultipartFile file) throws Exception {
        //获取上传文件名
        String fileName = file.getOriginalFilename();
        //获得随机文件名
        UUID uuid = UUID.randomUUID();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = uuid + suffix;
        //    图片常见的格式有：bmp、jpg、tiff、gif、pcx、tga、exif、fpx、svg、psd、cdr、pcd、dxf、ufo、eps、ai、raw等
        if (!suffix.equals(".jpg") & !suffix.equals(".gif") & !suffix.equals(".png") & !suffix.equals(" eg") &
                !suffix.equals(".webp") & !suffix.equals(".mp4")) {
            return Result.error("请传入正确格式文件");
        }
        //上传到OSS存储
        String url = AliyunOSS.uploadFile("Avatar/" + fileName, file.getInputStream());
        //将头像的地址信息保存到mysql

        return new Result<String>(0, "头像上传成功", url);
    }

    @Operation(summary = "资料上传")
    @PostMapping("/uploadData")
    //资料上传data
    public Result<String> uploadData(MultipartFile file) throws Exception {
    //获取上传文件名
        String fileName = file.getOriginalFilename();
        //获得随机文件名
//        UUID uuid = UUID.randomUUID();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
//        fileName = uuid + suffix;
        //    图片常见的格式有：bmp、jpg、tiff、gif、pcx、tga、exif、fpx、svg、psd、cdr、pcd、dxf、ufo、eps、ai、raw等
        if (!suffix.equals(".jpg") & !suffix.equals(".gif") & !suffix.equals(".png") & !suffix.equals(" eg") &
                !suffix.equals(".webp") & !suffix.equals(".mp4")& !suffix.equals(".txt")& !suffix.equals(".flv")
                & !suffix.equals(".avi")& !suffix.equals(".wav")& !suffix.equals(".doc")) {
            return Result.error("请传入正确格式文件");
        }
        //上传到OSS存储
        String url = AliyunOSS.uploadFile("Data/" + fileName, file.getInputStream());
        //将资料图片的地址信息保存到mysql

        return new Result<String>(0, "资料上传成功", url);
    }
}
